| Conditions | 10 |
| Total Lines | 67 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like login.js ➔ login often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | var express = require('express'); |
||
| 35 | function login(res, body) { |
||
| 36 | const email = body.email; |
||
| 37 | const password = body.password; |
||
| 38 | |||
| 39 | if (!email || !password) { |
||
| 40 | return res.status(401).json({ |
||
| 41 | errors: { |
||
| 42 | status: 401, |
||
| 43 | source: "/login", |
||
| 44 | title: "Email or password missing", |
||
| 45 | detail: "Email or password missing in request" |
||
| 46 | } |
||
| 47 | }); |
||
| 48 | } |
||
| 49 | |||
| 50 | db.get("SELECT * FROM users WHERE email = ?", |
||
| 51 | email, |
||
| 52 | (err, rows) => { |
||
| 53 | if (rows === undefined) { |
||
| 54 | return res.status(401).json({ |
||
| 55 | errors: { |
||
| 56 | status: 401, |
||
| 57 | source: "/login", |
||
| 58 | title: "User not found", |
||
| 59 | detail: "User with provided email not found." |
||
| 60 | } |
||
| 61 | }); |
||
| 62 | } |
||
| 63 | const user = rows; |
||
| 64 | |||
| 65 | bcrypt.compare(password, user.password, (err, result) => { |
||
| 66 | if (err) { |
||
| 67 | return res.status(500).json({ |
||
| 68 | errors: { |
||
| 69 | status: 500, |
||
| 70 | source: "/login", |
||
| 71 | title: "bcrypt error", |
||
| 72 | detail: "bcrypt error" |
||
| 73 | } |
||
| 74 | }); |
||
| 75 | } |
||
| 76 | |||
| 77 | if (result) { |
||
| 78 | let payload = { email: user.email }; |
||
| 79 | let jwtToken = jwt.sign(payload, jwtSecret, { expiresIn: '24h' }); |
||
| 80 | |||
| 81 | return res.json({ |
||
| 82 | data: { |
||
| 83 | type: "success", |
||
| 84 | message: "User logged in", |
||
| 85 | user: payload, |
||
| 86 | token: jwtToken |
||
| 87 | } |
||
| 88 | }); |
||
| 89 | } |
||
| 90 | |||
| 91 | return res.status(401).json({ |
||
| 92 | errors: { |
||
| 93 | status: 401, |
||
| 94 | source: "/login", |
||
| 95 | title: "Wrong password", |
||
| 96 | detail: "Password is incorrect." |
||
| 97 | } |
||
| 98 | }); |
||
| 99 | }); |
||
| 100 | }); |
||
| 101 | } |
||
| 102 | |||
| 104 |